package com.bw.liuyushan.liuyushan20210929.MVP.Fragment;

import android.content.Context;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bumptech.glide.Glide;
import com.bw.liuyushan.liuyushan20210929.Adapter.TabRVAdapter;
import com.bw.liuyushan.liuyushan20210929.Adapter.TabRVAdapter2;
import com.bw.liuyushan.liuyushan20210929.Adapter.TabRVAdapter3;
import com.bw.liuyushan.liuyushan20210929.Entity.JsonBean;
import com.bw.liuyushan.liuyushan20210929.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.liuyushan20210929.MVP.Model.BaseModelTest;
import com.bw.liuyushan.liuyushan20210929.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.liuyushan20210929.R;
import com.bw.liuyushan.mvp_lib.View.BaseFragment;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Liuyushan20210929
 * file name is : HomeFragment
 * created by Ender on 2021/9/29 14:00.
 * author : 刘雨杉
 * Describe:
 */
public class TabFragment extends BaseFragment<BasePresenterTest> implements IContrackTest.C_ViewTest, OnRefreshLoadMoreListener {
    private Banner ban;
    private SmartRefreshLayout smHome;
    private RecyclerView rvHome;
    ArrayList<Integer> integers;
    int page = 1;
    private RecyclerView rvFirst;
    private RecyclerView rvSecond;
    TabRVAdapter3 adapter3;
    boolean isAdapter;


    @Override
    public int bindLayout() {
        return R.layout.fragment_tabone;
    }

    @Override
    public void initView() {
        //防止内存泄露
        mPresenter = new BasePresenterTest(new BaseModelTest(), this);

        ban = (Banner) findViewById(R.id.ban);
        smHome = (SmartRefreshLayout) findViewById(R.id.sm_home);
        rvHome = (RecyclerView) findViewById(R.id.rv_home);
        rvFirst = (RecyclerView) findViewById(R.id.rv_first);
        rvSecond = (RecyclerView) findViewById(R.id.rv_second);

        smHome.setOnRefreshLoadMoreListener(this);
        //瀑布流
        rvHome.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        rvFirst.setLayoutManager(new LinearLayoutManager(getContext()));
        rvSecond.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    public void initDate() {
        mPresenter.mpDate("ios/cf/dish_list.php?stage_id=1&limit=20&page=" + page);
        initBanner();
    }

    @Override
    public void ShowDate(JsonBean jsonBean) {
        List<JsonBean.DataBean> data = jsonBean.getData();
        initAdapter(data);
        TabRVAdapter tabRVAdapter = new TabRVAdapter(data);
        rvFirst.setAdapter(tabRVAdapter);
        TabRVAdapter2 tabRVAdapter2 = new TabRVAdapter2(data);
        rvSecond.setAdapter(tabRVAdapter2);
    }

    private void initAdapter(List<JsonBean.DataBean> data) {
            smHome.finishLoadMore();
            smHome.finishRefresh();
            if (adapter3==null){
                adapter3=new TabRVAdapter3(data);
                rvHome.setAdapter(adapter3);
            }else{
                if (isAdapter){
                    adapter3.getData().clear();
                }
                adapter3.getData().addAll(data);
                adapter3.notifyDataSetChanged();
            }
    }

    private void initBanner() {
        integers = new ArrayList<>();
        integers.add(R.drawable.ix_1);
        integers.add(R.drawable.ix_2);
        integers.add(R.drawable.ix_3);
        integers.add(R.drawable.ix_4);
        integers.add(R.drawable.ix_5);
        ban.setImages(integers);
        ban.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        });
        ban.start();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isAdapter=false;
        page++;
        mPresenter.mpDate("ios/cf/dish_list.php?stage_id=1&limit=20&page=" + page);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isAdapter=true;
        page=1;
        mPresenter.mpDate("ios/cf/dish_list.php?stage_id=1&limit=20&page=" + page);
    }
}
